/**
 * @ Author: luoqi
 * @ Create Time: 2024-07-23 22:44
 * @ Modified by: luoqi
 * @ Modified time: 2024-07-23 23:08
 * @ Description:
 */
#include <stdlib.h>
#include <timer.h>
#include <bsp.h>
#include "bldc.h"
#include "../qshell/qterm.h"

static QCmdObj _cmd_bldc;
static int _cmd_bldc_hdl(int argc, char **argv);

int bldc_init(void)
{
    qterm_attach(&_cmd_bldc, "bldc", _cmd_bldc_hdl, "");

    return 0;
}

static void _cmd_bldc_help()
{
    QSH(" -pwm <start/stop>\r\n");
    QSH(" -pwm <ch> <duty>\r\n");
}

int _cmd_bldc_hdl(int argc, char **argv)
{
    if(ISARG(argv[1], "?") && argc == 2) {
        _cmd_bldc_help();
    } else if(ISARG(argv[1], "pwm") && argc == 3) {
        if(ISARG(argv[2], "start")) {
            timer_pwm_start(TIMER_DEV_TIM1, TIMER_CHANNEL_CH1);
            timer_pwm_start(TIMER_DEV_TIM1, TIMER_CHANNEL_CH2);
            timer_pwm_start(TIMER_DEV_TIM1, TIMER_CHANNEL_CH3);
        } else if(ISARG(argv[2], "stop")) {
            timer_pwm_stop(TIMER_DEV_TIM1, TIMER_CHANNEL_CH1);
            timer_pwm_stop(TIMER_DEV_TIM1, TIMER_CHANNEL_CH2);
            timer_pwm_stop(TIMER_DEV_TIM1, TIMER_CHANNEL_CH3);
        } else {
            return -1;
        }
    } else if(ISARG(argv[1], "pwm") && argc == 4) {
        if(ISARG(argv[2], "1")) {
            timer_pwm_set(TIMER_DEV_TIM1, TIMER_CHANNEL_CH1, atoi(argv[3]));
        } else if(ISARG(argv[2], "2")) {
            timer_pwm_set(TIMER_DEV_TIM1, TIMER_CHANNEL_CH2, atoi(argv[3]));
        } else if(ISARG(argv[2], "3")) {
            timer_pwm_set(TIMER_DEV_TIM1, TIMER_CHANNEL_CH3, atoi(argv[3]));
        } else {
            return -1;
        }
    } else {
        return -1;
    }
    return 0;
}
